<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="明星姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="明星姓名"></el-input>
      </el-form-item>
      <el-form-item label="出生年月" prop="birthday">
        <el-date-picker
          v-model="dataForm.birthday"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd  HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="    性别" prop="sex">
        <el-switch
          style="display: block"
          v-model="dataForm.sex"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="男"
          inactive-text="女"
          :active-value="1"
          :inactive-value="2"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label=" 状态" prop="status">
        <el-switch
          style="display: block"
          v-model="dataForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="活跃"
          inactive-text="禁用"
          :active-value="0"
          :inactive-value="1"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="简介" prop="introduce">
        <el-input v-model="dataForm.introduce" placeholder="简介"></el-input>
      </el-form-item>
     <el-form-item label="国家" prop="introduce">
        <el-input v-model="dataForm.addr" placeholder="国家"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="picture">
        <el-upload
          class="avatar-uploader"
          :action="getUploadUrl"
          :show-file-list="false"
          :with-credentials="true"
          :headers="header"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          field="file"
        >
          <img
            v-if="dataForm.picture"
            :src="dataForm.picture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="明星类型" prop="type">
        <el-switch
          style="display: block"
          v-model="dataForm.type"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="演员"
          inactive-text="导演"
          :active-value="0"
          :inactive-value="1"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="身高" prop="height">
        <el-input v-model="dataForm.height" placeholder="身高"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      header: {},
      imageUrl: "",
      visible: false,
      dataForm: {
        id: 0,
        createTime: "",
        birthday: "",
        sex: "",
        deleted: "",
        status: "",
        introduce: "",
        name: "",
        picture: "",
        type: "",
        height: "",
        addr: "",
      },
      dataRule: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        birthday: [
          { required: true, message: "出生年月不能为空", trigger: "blur" },
        ],
        sex: [
          {
            required: true,
            message: "    性别 1.男 2.女不能为空",
            trigger: "blur",
          },
        ],
        deleted: [
          {
            required: true,
            message:
              "     * 订单状态 1.待支付 2.支付成功 3.申请退单 4.退单成功 5.退单失败 6.订单取消不能为空",
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: "     * 状态：1上架 2下架 默认：1不能为空",
            trigger: "blur",
          },
        ],
        introduce: [
          { required: true, message: "简介不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "明星姓名不能为空", trigger: "blur" },
        ],
        picture: [{ required: true, message: "图片不能为空", trigger: "blur" }],
        type: [
          {
            required: true,
            message: "    明星类型：1.演员 2。导演不能为空",
            trigger: "blur",
          },
        ],
        height: [{ required: true, message: "身高不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
      handleAvatarSuccess(res, file) {
      console.log(res);
      this.dataForm.picture = res.imageUrl;
    },
    beforeAvatarUpload(file) {
      var ca = document.cookie.split(";");
      console.log(ca[0].split("="));
      this.header.token = ca[0].split("=")[1];

      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      console.log(isJPG && isLt2M);
      return isJPG && isLt2M;
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/cb/star/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.addr=data.star.addr;
              this.dataForm.createTime = data.star.createTime;
              this.dataForm.birthday = data.star.birthday;
              this.dataForm.sex = data.star.sex;
              this.dataForm.deleted = data.star.deleted;
              this.dataForm.status = data.star.status;
              this.dataForm.introduce = data.star.introduce;
              this.dataForm.name = data.star.name;
              this.dataForm.picture = data.star.picture;
              this.dataForm.type = data.star.type;
              this.dataForm.height = data.star.height;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/cb/star/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              createTime: this.dataForm.createTime,
              birthday: this.dataForm.birthday,
              sex: this.dataForm.sex,
              deleted: this.dataForm.deleted,
              status: this.dataForm.status,
              introduce: this.dataForm.introduce,
              name: this.dataForm.name,
              picture: this.dataForm.picture,
              type: this.dataForm.type,
              height: this.dataForm.height,
              addr : this.dataForm.addr,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
    computed: {
    getUploadUrl() {
      return window.SITE_CONFIG.baseUrl + "/cb/file/upload";
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>